#include "authwindow.h"
#include "ui_authwindow.h"
#include "mainwindow.h"
#include "usermanager.h"
#include <QMessageBox>

AuthWindow::AuthWindow(QWidget *parent)
    : QDialog(parent)
    , authwindowUI(new Ui::AuthWindow)
    , networkManager(new NetworkManager(this))
{
    authwindowUI->setupUi(this);
    authwindowUI->roleComboBox->addItems({"user", "admin"});

    connect(networkManager, &NetworkManager::loginResponseReceived, this, &AuthWindow::handleLoginResponse);
    connect(networkManager, &NetworkManager::registerResponseReceived, this, &AuthWindow::handleRegisterResponse);
}

AuthWindow::~AuthWindow()
{
    delete authwindowUI;
}

void AuthWindow::on_loginButton_clicked()
{
    QString username = authwindowUI->usernamelineEdit_1->text();
    QString password = authwindowUI->passwordlineEdit_1->text();
    QString role = authwindowUI->roleComboBox->currentText();
//#define DEBUG
#ifndef DEBUG
    QJsonObject json;
    json["username"] = username;
    json["password"] = password;
    json["role"] = role;

    if (username.isEmpty() || password.isEmpty())
        return;
#else
    QJsonObject json;
    json["username"] = "admin";
    json["password"] = "1234";
    json["role"] = "admin";
#endif
    networkManager->loginUser(json);
}

void AuthWindow::on_registerButton_clicked()
{
    QString username = authwindowUI->usernamelineEdit_2->text();
    QString password = authwindowUI->passwordlineEdit_2->text();
    QString confirmPassword = authwindowUI->comfirmPasswdLineEdit->text();

    if (username.isEmpty() || password.isEmpty())
        return;

    if (password != confirmPassword) {
        QMessageBox::warning(this, "错误", "密码和确认密码不匹配");
        return;
    }

    QJsonObject json;
    json["username"] = username;
    json["password"] = password;
    networkManager->registerUser(json);
}

void AuthWindow::on_exitButton_1_clicked()
{
    close();
}

void AuthWindow::on_exitButton_2_clicked()
{
    close();
}

/*******************************
 *
 *   Response Data Handler
 *
 ******************************/

void AuthWindow::handleLoginResponse(const QJsonObject &response) {

    int code = response.value("code").toInt();
    QString desc = response.value("desc").toString();

    if (code == 200) {
        QMessageBox::information(this, "登录成功", desc);
        close();

        UserManager::instance().setUserInfo(
            authwindowUI->usernamelineEdit_1->text(),
            authwindowUI->passwordlineEdit_1->text(),
            authwindowUI->roleComboBox->currentText() == "admin" ?
                UserManager::Admin : UserManager::User
        );

        MainWindow *mainWindow = new MainWindow();
        mainWindow->show();
    } else {
        QMessageBox::warning(this, "登录失败", desc);
    }
}

void AuthWindow::handleRegisterResponse(const QJsonObject &response) {
    int code = response.value("code").toInt();
    QString desc = response.value("desc").toString();

    if (code == 200) {
        QMessageBox::information(this, "注册成功", desc);
    } else {
        QMessageBox::warning(this, "注册失败", desc);
    }
}
